package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.TenderDictEntity3;
import com.sgb.kjwl.view.ui.activity.pick.PhotoDownloadViewActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

public class PickDetailFileAdapter extends BaseRecycleAdapter<TenderDictEntity3> {

    private Context context;
    public String title;

    public PickDetailFileAdapter(Context context, List<TenderDictEntity3> datas,String title) {
        super(context, R.layout.item_pick_detial_file, datas);
        this.context = context;
        this.title = title;
    }


    @Override
    public void convert(ItemViewHolder holder, final TenderDictEntity3 entity) {
        holder.setBinding(BR.viewModel, entity);
        ImageView imageFile = holder.getItem().findViewById(R.id.image_file);
        TextView tvType = holder.getItem().findViewById(R.id.tv_type);

        if (entity.getFileUrl().contains(".png") || entity.getFileUrl().contains(".jpg") || entity.getFileUrl().contains(".gif")) {
            tvType.setText("预览");
            imageFile.setImageResource(R.mipmap.fujian_yulan);
            tvType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(entity.getFileUrl())) {
                        Intent intent = new Intent(context, PhotoDownloadViewActivity.class);
                        intent.putExtra("url", entity.getFileUrl());
                        intent.putExtra("title",title);
                        context.startActivity(intent);
                    }
                }
            });
        } else {
            tvType.setText("下载");
            imageFile.setImageResource(R.mipmap.fujian_xiazai);
            tvType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //调用系统浏览器下载
                    if (!TextUtils.isEmpty(entity.getFileUrl())) {
                        Uri uri = Uri.parse(entity.getFileUrl());
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        context.startActivity(intent);
                    }
                }
            });
        }
    }

}
